package com.example.showpicture1;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.viewpager.widget.ViewPager;

import com.example.showpicture1.adapter.ViewPagerAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * author : Lele
 * date   : 2022/7/13 9:28
 * desc   :
 * version: 1.0
 */
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    TextView tv_page;
    int sum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //提示获取权限
        String[] PERMISSIONS = {Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE};
        int PERMISSION_CODE = 123;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            if (ActivityCompat.checkSelfPermission(MainActivity.this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS, PERMISSION_CODE);
            }
        }
        //将图片名用list存放
        List<String> mList = getAllDataFileName("/storage/emulated/0/ImageForShow");
        Log.i("lele", "加载了: " + mList.size() + "张图片");
        //隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        tv_page = findViewById(R.id.tv_bigimage_index);
        if (sum != 0) {
            tv_page.setText("1/" + sum);
        } else {
            tv_page.setText("没有找到图片");
        }
        ViewPager viewPager = findViewById(R.id.vp);
        viewPager.addOnPageChangeListener(this);
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this, mList);
        viewPager.setAdapter(viewPagerAdapter);
        ImageView im_btn=findViewById(R.id.imageButton);
        im_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //获取图片名称
    public ArrayList<String> getAllDataFileName(String folderPath) {
        ArrayList<String> fileList = new ArrayList<>();
        File file = new File(folderPath);
        File[] tempList = file.listFiles();
        sum=tempList.length;
        if (sum == 0) {
            Log.i("lele", "getAllDataFileName: 文件为空");
            return null;
        }

        //对图片格式进行筛选
        for (int i = 0; i < sum; i++) {
            if (tempList[i].isFile()) {
                Log.i("lele", "getAllDataFileName: 图片" + i + ":" + tempList[i].getName());
                String fileName = tempList[i].getName();
                //  根据自己的需要进行类型筛选
                if (fileName.endsWith(".jpg")) {
                    fileList.add(fileName);
                } else if (fileName.endsWith(".png")) {
                    fileList.add(fileName);
                } else if (fileName.endsWith(".jpeg")) {
                    fileList.add(fileName);
                } else if (fileName.endsWith(".webp")) {
                    fileList.add(fileName);
                } else if (fileName.endsWith(".svg")) {
                    fileList.add(fileName);
                } else if (fileName.endsWith(".gif")) {
                    fileList.add(fileName);
                }
            }
        }
        return fileList;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        tv_page.setText((position+1)+"/"+sum);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}